import { verify, sign } from "jsonwebtoken";
import { TOKEN_SECRET, EXPIRESIN } from "../config";

class Auth {
  // 生产token
  public generateToken(info: any, scope: number = 1) {
    const token = sign(
      {
        time: Date.now(),
        timeout: Date.now() + EXPIRESIN,
        ...info,
        scope,
      },
      TOKEN_SECRET,
      {
        expiresIn: EXPIRESIN,
      }
    );
    return token;
  }
  // 验证token
  public verifyToken(token: string) {
    try {
      return verify(token, TOKEN_SECRET);
    } catch (e) {
      return false;
    }
  }
}

export const auth = new Auth();
